package com.dqx.haoqiu.mvp.module.login;

import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.common.base.BasePresenter;
import com.dqx.haoqiu.repository.repository.user.UserRepository;
import com.xmjs.baselibrary.network.retrofit.RxUtil;

import javax.inject.Inject;

public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {
    UserRepository userRepository;

    @Inject
    public LoginPresenter(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public void login(String mobile, String password) {
        userRepository.login(mobile, password)
                .compose(RxUtil.applySchedulers())
                .compose(RxUtil.bindToLifecycle(view))
                .doOnSubscribe(disposable -> view.showLoadingView(true))
                .doAfterTerminate(() -> view.hideLoadingView())
                .subscribe(entity -> {
                    view.loginSuccess();
                }, ex -> {
                    showNetworkError(ex, R.string.def_error_message, view);
                });
    }
}